package com.F05.dao;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.log4j.Logger;
import com.F05.actionform.F0502ActionForm;
import com.dao.DataOperations;
import com.dbtools.Dispose;
import com.exception.SelectException;
import com.util.CommonTools;

public class F0502DAO extends DataOperations{
	Logger log = Logger.getLogger(F0502DAO.class);
	public F0502DAO(){
		
	}
    public F0502DAO(Connection connection){
        super(connection);
    }
    
    public List queryList(F0502ActionForm actionForm) throws SelectException{
    	List list = new ArrayList();
    	StringBuffer sql = new StringBuffer();
    	sql.append(" select id,year,cost ");
        sql.append(" from f_manufacture_cost ");
        sql.append(" where 1=1 ");
 
    	if(!actionForm.getYear1().equals("")){
    		sql.append(" and year >= '"+actionForm.getYear1()+"'");
    	}
    	if(!actionForm.getYear2().equals("")){
    		sql.append(" and year <= '"+actionForm.getYear2()+"'");
    	}

    	ResultSet rest = null;
    	try{
    		log.info(sql.toString());
    		rest = this.executeQuery(sql.toString());
    		while(rest.next()){
    			Map map = new HashMap();
    			map.put("ID", CommonTools.null2String(rest.getString("id")));
    			map.put("year", CommonTools.null2String(rest.getString("year")));
    			map.put("cost", CommonTools.null2String(rest.getString("cost")));
			
				list.add(map);
    		}
    	}catch(SQLException e){
    		e.printStackTrace();
    	}finally{
			Dispose.dispose(rest);
		}
    	return list;
    }
    
    public boolean delete(String id,F0502ActionForm form){
		boolean flag=true;
		StringBuffer sql=new StringBuffer();
		List sqlList = new ArrayList();
		sql.append("DELETE FROM f_manufacture_cost  ");
		sql.append("WHERE id ="+id);
		sqlList.add(sql.toString());
		try{
			log.info("sql is "+sql.toString());
			flag=super.executeUpdate(sqlList,true);
			form.setErrorFlag("0");
		}catch(Exception e){
			e.printStackTrace();
			form.setErrorFlag("1");
		}
		return flag;
	}
    
    public boolean insert(F0502ActionForm form){
		boolean flag=false;
		StringBuffer sql=new StringBuffer();
		List sqlList = new ArrayList();
		try{
			sql.append("INSERT INTO f_manufacture_cost  ");
			sql.append("  (year,cost ) ");
			sql.append("VALUES ('"+form.getYear()+"', '"+form.getCost()+"')");
			
			sqlList.add(sql.toString());
			log.info("sql is "+sql.toString());
			flag=super.executeUpdate(sqlList,true); 
			form.setErrorFlag("0");
		}catch(SQLException ex){
			if("1".equals(String.valueOf(ex.getErrorCode()))){
				//form.setErrorFlag("主键重复,错误序号: "+String.valueOf(ex.getErrorCode()));
				form.setErrorFlag("3");
			}else{
				form.setErrorFlag("1");
			}
		}catch(Exception e){
			e.printStackTrace();
			form.setErrorFlag("1");
		}
		return flag;
	}
    
    public List selectById(String id) throws SelectException{
    	List list = new ArrayList();
    	StringBuffer sql = new StringBuffer();    	
    	
    	sql.append("SELECT id,year,cost  FROM f_manufacture_cost ");
    	sql.append(" where id = ").append(id);
    	ResultSet rest = null;
    	try {
    		log.info("sql is "+sql);
    		rest = this.executeQuery(sql.toString());
			while(rest.next()){
				Map map = new HashMap();
				map.put("ID", CommonTools.null2String(rest.getString("id")));
    			map.put("year", CommonTools.null2String(rest.getString("year")));
    			map.put("cost", CommonTools.null2String(rest.getString("cost")));
	
				list.add(map);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			Dispose.dispose(rest);
		}
		return list;
    }
    
    public boolean update(F0502ActionForm form){
    	boolean flag =false;
    	StringBuffer sql=new StringBuffer();
		List sqlList = new ArrayList();
		sql.append("update f_manufacture_cost set ");
		sql.append(" year='"+form.getYear()+"',");
		sql.append(" cost='"+form.getCost()+"'");
		sql.append(" where id="+form.getId());
		sqlList.add(sql.toString());
		
		try{
			log.info("sql is "+sql.toString());
			flag=super.executeUpdate(sqlList,true); 	
			form.setErrorFlag("0");
		}catch(Exception e){
	        e.printStackTrace();
	        form.setErrorFlag("1");
		}
		return flag;
    }
    
}
